﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class SiteVersion : BaseItem
    {
        private SiteVersion() : base() { }
        private SiteVersion(Hashtable table) : base(table) { }
        private SiteVersion(Hashtable table, List<SiteVersion> collection) : base(table, collection) { }

        public static SiteVersion CreateInstance()
        {
            return new SiteVersion();
        }

        private static SiteVersion Get(Hashtable table)
        {
            if (table != null)
                return new SiteVersion(table);
            else
                return null;
        }

        private static List<SiteVersion> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<SiteVersion> tmp = new List<SiteVersion>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new SiteVersion(table, tmp));
                }
                return tmp;
            }
            else
                return null;
        }

        protected override void Load(Hashtable table)
        {
            Change = (DateTime)table[SiteVersionDal.Fields.Change];
        }

        public DateTime Change { get; private set; }

        protected override Base CreateDal()
        {
            return new SiteVersionDal();
        }

        public static List<SiteVersion> GetAll(int siteId)
        {
            SiteVersionDal dal = new SiteVersionDal();
            return Get(dal.GetAll(siteId));
        }

        public static List<SiteVersion> GetAllFuture(int siteId)
        {
            SiteVersionDal dal = new SiteVersionDal();
            return Get(dal.GetAllFuture(siteId));
        }

        public static List<SiteVersion> GetAll(int siteId, DateTime? startDate, DateTime? endDate)
        {
            SiteVersionDal dal = new SiteVersionDal();
            return Get(dal.GetRange(siteId, startDate, endDate));
        }

        public override string ToString()
        {
            return Change.ToString("yyyy/MM/dd HH:mm");
        }

        public static SiteVersion GetCurrent(int siteId)
        {
            SiteVersionDal dal = new SiteVersionDal();
            return Get(dal.GetCurrent(siteId));
        }


        public static SiteVersion GetCurrentBySiteUrl(string siteUrl)
        {
            SiteVersionDal dal = new SiteVersionDal();
            return Get(dal.GetCurrentBySiteUrl(siteUrl));
        }
    }
}
